<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-center">
      <el-row class="jsbos-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="4">
            <el-form-item label="发送渠道">
              <el-select
                v-model="query.channelType"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="(item, index) in channelTypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="接收人">
              <user-select
                v-model="query.sysUserIds"
                placeholder="请选择"
                clearable
                :multiple="true"
              >
              </user-select>
            </el-form-item>
          </el-col>
          <template v-if="showAll">
            <el-col :span="4">
              <el-form-item label="状态">
                <el-select
                  v-model="query.enabledMark"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in enabledMarkOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
              <el-button
                type="text"
                icon="el-icon-arrow-down"
                @click="showAll = true"
                v-if="!showAll"
              >
                展开
              </el-button>
              <el-button
                type="text"
                icon="el-icon-arrow-up"
                @click="showAll = false"
                v-else
              >
                收起
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              :disabled="multiple"
              @click="handleAgainSendMsgBatch()"
              >批量重发
            </el-button>
          </div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JSBOS-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
          </div>
        </div>
        <jsbos-table
          v-loading="listLoading"
          :data="list"
          :border="true"
          :isHeader="false"
          :hasC="true"
          style="margin-left: 5px"
          @selection-change="handleSelectionChange"
          @sort-change="sortChange"
        >
          <!-- <el-table-column
            prop="msgId"
            label="消息内容"
            width="200"
            :align="'left'"
            :show-overflow-tooltip="true"
          /> -->
          <el-table-column
            label="发送渠道"
            width="100"
            prop="channelType"
            algin="left"
          >
            <template slot-scope="scope">
              {{ getChannelName(scope.row.channelType) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="sysUserId"
            label="接收人"
            width="100"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="receiveUserId"
            label="thirdID"
            width="200"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="enabledMark"
            label="状态"
            width="100"
            algin="'left'"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <el-tag
                :type="
                  scope.row.enabledMark == 0
                    ? 'warning'
                    : scope.row.enabledMark == 1
                    ? 'success'
                    : 'danger'
                "
                disable-transitions
              >
                {{ scope.row.enabledMark | dynamicText(enabledMarkOptions) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="sendTime"
            label="发送时间"
            width="180"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="sendUserId"
            label="发送人"
            width="100"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="description"
            label="备注描述"
            min-width="300"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="againTime"
            label="重发时间"
            width="180"
            :align="'left'"
          />
          <el-table-column
            prop="againUserId"
            label="重发人"
            width="100"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="是否阅读"
            width="100"
            prop="isRead"
            algin="left"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.isRead == 1 ? 'success' : 'danger'"
                disable-transitions
              >
                {{ scope.row.isRead | dynamicText(isReadOptions) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="readTime"
            label="阅读时间"
            width="180"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="readCount"
            label="阅读次数"
            width="100"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="操作" fixed="right" width="50">
            <template slot-scope="scope">
              <el-button
                v-show="
                  scope.row.enabledMark != 1 || scope.row.channelType != '1'
                "
                type="text"
                @click="handleAgainSendMsgBatch(scope.row)"
                >重发
              </el-button>
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getList, againSend } from "@/views/message/api/mcTaskMsgReceive";

export default {
  components: {},
  props: {
    menuId: {
      type: String,
      default: "",
    },
    msgId: {
      type: String,
      default: "",
    },
    channelType: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      showAll: false,
      detailVisible: false,
      query: {
        msgId: this.msgId,
        channelType: this.channelType,
        receiveUserId: undefined,
        sysUserId: undefined,
        sysUserIds: [],
        enabledMark: undefined,
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "enabledMark",
      },
      channelTypeOptions: [],
      enabledMarkOptions: [
        { fullName: "发送失败", id: "2" },
        { fullName: "发送成功", id: "1" },
        { fullName: "待下发", id: "0" },
      ],
      enabledMarkProps: {
        label: "fullName",
        value: "id",
      },
      isReadOptions: [
        { fullName: "是", id: "1" },
        { fullName: "否", id: "0" },
      ],
      isReadProps: {
        label: "fullName",
        value: "id",
      },

      // 非多个禁用
      multiple: true,
      // 选中数组
      multipleSelection: [],
      // 选中ids数组
      ids: [],
      receiveUser: [],
    };
  },
  watch: {
    msgId() {
      this.query.msgId = this.msgId;
      this.initData();
    },
    channelType() {
      this.query.channelType = this.channelType;
      this.initData();
    },
  },
  created() {
    this.initData();
    this.getMessageTypeOptions();
  },
  methods: {
    // 渠道名
    getChannelName(e) {
      for (var i = 0; i < this.channelTypeOptions.length; i++) {
        if (this.channelTypeOptions[i].enCode == e) {
          return this.channelTypeOptions[i].fullName;
        }
      }
    },
    getMessageTypeOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "msgSendType",
        })
        .then((res) => {
          this.channelTypeOptions = res;
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
      this.ids = selection.map((item) => item.id);
      this.receiveUser = selection.map((item) => item.sysUserId);
      this.multiple = !selection.length;
    },
    /** 批量重发 */
    handleAgainSendMsgBatch(row) {
      const receiveInfo = row || this.multipleSelection;
      let receiveIds;
      let receiveUser;
      if (receiveInfo instanceof Array) {
        try {
          receiveInfo.forEach((item) => {
            if (item.enabledMark == 1 && item.channelType === "1") {
              throw new Error("请选择非‘站内信’数据"); //报错，就跳出循环
            }
          });
        } catch (e) {
          this.$message({
            message: e.message,
            type: "warning",
            duration: 1000,
          });
          return;
        }
        receiveIds = this.ids;
        receiveUser = this.receiveUser;
      } else {
        if (receiveInfo.enabledMark == 1 && receiveInfo.channelType === "1") {
          this.$message({
            message: "请选择非‘站内信’数据",
            type: "warning",
            duration: 1000,
          });
          return;
        }
        receiveIds = receiveInfo.id;
        receiveUser = receiveInfo.sysUserId;
      }

      this.$confirm("确认批量重发'" + receiveUser + "'的数据？", "提示", {
        type: "warning",
      })
        .then(() => {
          if (this.msgId && receiveIds) {
            againSend(this.msgId, receiveIds).then((res) => {
              this.$message({
                type: "success",
                message: res.msg,
                onClose: () => {
                  this.initData();
                },
              });
            });
          }
        })
        .catch(() => {});
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      if (this.query.msgId) {
        this.listLoading = true;
        let _query = {
          ...this.listQuery,
          ...this.query,
          menuId: this.menuId,
        };
        getList(_query).then((res) => {
          var _list = [];
          for (let i = 0; i < res.data.list.length; i++) {
            let _data = res.data.list[i];
            _list.push(_data);
          }
          this.list = _list;
          this.total = res.data.pagination.total;

          this.listLoading = false;
        });
      } else {
        this.list = [];
        this.listLoading = false;
      }
    },
    search() {
      this.initData();
    },
    reset() {
      this.resetQuery();
      this.resetListQuery();
      this.initData();
    },
    resetQuery() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
    },
    resetListQuery() {
      this.listQuery = {
        keyword: "",
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatorTime",
      };
    },
  },
};
</script>
